package com.example.jdai.jdai.ui;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.jdai.jdai.MyApplication;
import com.example.jdai.jdai.R;
import com.example.jdai.jdai.config.Config;
import com.example.jdai.jdai.rest.BackRest;
import com.example.jdai.jdai.ui.wig.ShengheTip;
import com.example.jdai.jdai.util.DataAll;
import com.example.jdai.jdai.util.Tools;
import com.sunfusheng.marqueeview.MarqueeView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindDrawable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ScActivity extends Activity implements BackRest.DoLong {

    //    @BindView(R.id.iv_tip)
//    ImageView ivTip;
//    @BindView(R.id.tv_tip)
//    TextView tvTip;
//    @BindView(R.id.activity_sc)
//    RelativeLayout activitySc;
//    @BindDrawable(R.drawable.tip_fail)
//    Drawable tip_fail;
//    @BindDrawable(R.drawable.tip_ing)
//    Drawable tip_ing;
//    @BindDrawable(R.drawable.tip_ok)
//    Drawable tip_ok;
//    @BindView(R.id.bt_iknow)
//    Button btIknow;
    int count = 0;
    @BindView(R.id.st_tip)
    ShengheTip stTip;
    AlertDialog ad;
    @BindView(R.id.tb)
    TopBar tb;
    @BindView(R.id.iv_show)
    ImageView ivShow;
    @BindDrawable(R.drawable.cs1)
    Drawable cs1;
    @BindDrawable(R.drawable.cs2)
    Drawable cs2;
    @BindDrawable(R.drawable.cs3)
    Drawable cs3;
    @BindView(R.id.iv_logo)
    MarqueeView ivLogo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Config.ISDEBUG)
            Log.v("sc", "sc");
        setContentView(R.layout.activity_sc);
        ButterKnife.bind(this);
        ivLogo.start();
        setTip(MyApplication.dataAll.data.get(DataAll.IS_COMMIT), MyApplication.dataAll.data.get(DataAll.COMMIT_C),
                MyApplication.dataAll.data.get(DataAll.COMMIT_V), MyApplication.dataAll.data.get(DataAll.MONEY_N),
                MyApplication.dataAll.data.get(DataAll.REASON_N)
                );
        Tools.Update(this);
        MyApplication.backRest.getScR(ScActivity.this);

        // firstTip();
        tb.setTopBarLisener(new TopBar.TopBarLisener() {
            @Override
            public void back() {
                if(!TextUtils.isEmpty(MyApplication.dataAll.data.get(DataAll.QQ_N))){
                    try {

                        String url = "mqqwpa://im/chat?chat_type=wpa&uin=" + MyApplication.dataAll.data.get(DataAll.QQ_N);
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    }
                    catch (Exception e){
                        Log.v("qq",e.getMessage());
                    }
                }
                else
                Toast.makeText(ScActivity.this,"暂时无法联系",Toast.LENGTH_SHORT).show();


            }

            @Override
            public void more() {
                startActivity(new Intent(ScActivity.this, AboutActivity.class));
            }
        });

        stTip.setReFill(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ScActivity.this, MainActivity.class).putExtra("isfail", true));
                finish();
            }
        });

    }

    void setTip(String id, String creatT, String virT, String n,String reason) {
        stTip.setTip(id, creatT, virT, n,reason);
        if ("3".equals(id)) {
            //ivShow.setImageDrawable(cs3);
        } else if ("2".equals(id)) {
            //ivShow.setImageDrawable(cs2);
        } else if ("1".equals(id)) {
            //ivShow.setImageDrawable(cs1);
        } else if ("0".equals(id)) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }


//         if("3".equals(id)){
//            ivTip.setImageDrawable(tip_ing);
//            tvTip.setText("正在审核中哦~");
//            btIknow.setText("我知道了");
//            btIknow.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    tip();
//                }
//            });
//            return;
//        }
//        else if("2".equals(id)){
//            ivTip.setImageDrawable(tip_fail);
//            tvTip.setText("审查失败");
//            btIknow.setText("重新填写");
//            btIknow.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    startActivity(new Intent(ScActivity.this,MainActivity.class));
//                    finish();
//                }
//            });
//            return;
//        }
//        else if ("1".equals(id)){
//            ivTip.setImageDrawable(tip_ok);
//            tvTip.setText("审查成功");
//            btIknow.setText("我知道了");
//            btIknow.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    AlertDialog.Builder builder = new AlertDialog.Builder(ScActivity.this);
//                    //    设置Title的图标
//                    builder.setIcon(R.drawable.logo);
//                    builder.setTitle("温馨提示");
//                    builder.setMessage("行走江湖，盘缠尽足！！！");
//                    //    设置一个PositiveButton
//                    ad=builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialogInterface, int i) {
//                            count++;
//                        }
//                    }).show();
//                }
//            });
//            return;
//        }
//        else if ("0".equals(id)||"".equals(id)){
//            MyApplication.dataAll.data.put(DataAll.IS_COMMIT,"");
//            MyApplication.dataAll.saveAll();
//            startActivity(new Intent(ScActivity.this,MainActivity.class));
//            return;
//        }


    }

    void tip() {
        String tip;
        if (count == 2) {
            tip = "恩！正在催促后台审核！";
        } else if (count == 5) {
            tip = "再催我重启！";
        } else if (count == 6) {
            tip = "骗你的！";
        } else {
            tip = "请耐心等待";
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //    设置Title的图标
        builder.setIcon(R.drawable.logo1);
        builder.setTitle("温馨提示");
        builder.setMessage(tip);
        //    设置一个PositiveButton
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                count++;
            }
        }).show();

    }

    void firstTip() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //    设置Title的图标
        builder.setIcon(R.drawable.logo1);
        builder.setTitle("温馨提示");
        builder.setMessage("您已经填写过资料！");
        //    设置一个PositiveButton
        ad = builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                count++;
            }
        }).show();

    }

    @Override
    protected void onPause() {
        super.onPause();
        MyApplication.dataAll.saveAll();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (ad != null)
            ad.dismiss();
        MyApplication.dataAll.saveAll();
    }

    @Override
    public void doDone(int mark, Map str) {
        try {
            String index = (String) str.get("commit");
            String creat = (String) str.get("commit_time");
            String ver = (String) str.get("verify_time");
            String money = (String) str.get("money");
            String reason = (String) str.get("reason");
            //String qq = (String) str.get("qq");
            setTip(index, creat, ver, money,reason);

        }catch (Exception e){

        }

            //MyApplication.dataAll.saveAll();
    }

    @OnClick(R.id.iv_show)
    public void onClick() {
    }
}
